'use strict';

const Controller = require('egg').Controller;
const jwt = require('jsonwebtoken');

class UserController extends Controller {
    //登录
    async login() {
        let {ctx,service} = this;
        let {username,password} = ctx.request.body;
        if(username && /\d{4,6}/.test(password)){
            //密码
            let hmcPwd = ctx.helper.hmc(password);
            let target = await service.user.login(username,hmcPwd);
            console.log(target);
            if(target.length){
                let token = jwt.sign({username,role_id:target[0].role_id},'1801a',{expiresIn:60*60*5})
                ctx.body ={ 
                    code:1,
                    msg:'登录成功',
                    token,
                    id:target[0].id,
                    username:target[0].username,
                    age:target[0].age
                }
            }else{
                ctx.body ={ 
                    code:0,
                    msg:'登录失败'
                }
            }
        }else{
            ctx.body = {
                code:0,
                msg:'参数有误'
            }
        }
    }

    async registry(){
        //参数  排重
        //注册
        // let target = await service.user.registry(xxx,xxx);
        // if(target.length){
        //     ctx.body ={
        //         code:1
        //     }
        // }else{
        //     ctx.body = {
        //         code:0,
        //     }
        // }

        // try{
            // let target = await service.user.registry(xxx,xxx);
        // }catch(e){
        //     ctx.body = {
        //         code:0,
        //         msg:e
        //     }
    
        // }
        
    }

    //获取菜单
    async list(){
        let {ctx} = this;
        let {role_id} = ctx.info;
        let list = await this.service.user.list(role_id);
        ctx.body = {
            code:1,
            data:list
        }
    }

    //修改
    async update(){
        let {ctx,service} = this;
        // {data:{username,password},id:2}  //111111
        console.log("ctx.params",ctx.params);
        let {data,id} = ctx.request.body;
        if(data && id){
            if(data.password){
                data.password = ctx.helper.hmc(data.password);
            }
            try{
                await service.user.update(data,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            } 
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //修改密码
    async editPwd(){
        let {ctx,service} = this;
        let {id,newPwd,oldPwd} = ctx.request.body;
        if(id && newPwd && oldPwd){
            //1.原密码输入的必须是对的
            //2.修改密码
            let hmcPwd = ctx.helper.hmc(oldPwd);
            let target = await service.user.selectPwd(id,hmcPwd);
            if(target.length){
                //原密码无问题
                try{
                    let pwd = ctx.helper.hmc(newPwd);
                    await service.user.editPwd(id,pwd);
                    ctx.body = {
                        code:1,
                        msg:'修改成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }else{
                ctx.body = {
                    code:0,
                    msg:'原密码错误'
                }
            }
        }else{
            ctx.body ={
                code:2,
                msg:'参数有误'
            }
        }
    }
}

module.exports = UserController;

//update userlist set username=?,password=?,role_id=? where id=?  ,[username,password,rle_id,id]  

//update userlist set password=? where id=?   ,[password,id]  

//update userlist set username=?,password=? where id=? ,[username,password,id]  
